package com.ohos.carbon.utils;

import com.ohos.carbon.ResourceTable;
import ohos.app.Context;

import java.security.SecureRandom;
import java.util.HashSet;
import java.util.Set;

public class RandomImageUtil {
    private Context context;
    private SecureRandom random = new SecureRandom();
    private boolean preventDuplicates;

    private Set<Integer> used = new HashSet<>();

    private int[] images = {
            ResourceTable.Media_randomdata_background0,
            ResourceTable.Media_randomdata_background1,
            ResourceTable.Media_randomdata_background2,
            ResourceTable.Media_randomdata_background3,
            ResourceTable.Media_randomdata_background4,
    };

    public RandomImageUtil(Context context) {
        this.context = context;
        preventDuplicates = true;
    }

    public RandomImageUtil(Context context, boolean preventDuplicates) {
        this.context = context;
        this.preventDuplicates = preventDuplicates;
    }
    public int next2() {
        int resId;
        if (preventDuplicates) {
            if (used.size() == images.length)
                used.clear();
            do {
                resId = images[random.nextInt(images.length)];
            }
            while (used.contains(resId));
            used.add(resId);
        } else {
            resId = images[random.nextInt(images.length)];
        }
        return resId;
    }

}
